<?php
//$Id: view.php 397 2012-05-27 08:20:57Z tomqin@gmail.com $

/**
 * The View Class
 *
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

final class View{

    private static $data = array();
    private static $tpls = array();
    private static $tplRoot = '';

    /**
     * Set the root path of the template
     *
     * @param string
     * @return void
     */
    public static function setTplRoot($root){
        self::$tplRoot = Core::getRealPath($root, '/');
    }

    /**
     * Set a value for template
     *
     * @param string
     * @param mixed
     * @return void
     */
    public static function setVar($key, $var = ''){
        self::$data[$key] = $var;
    }

    /**
     * Get a value from template
     *
     * @param string
     * @return mixed
     */
    public static function getVar($key){
        return isset(self::$data[$key]) ? self::$data[$key] : null;
    }

    /**
     * Add a view code to template
     *
     * @param string
     * @return void
     */
    public static function addView($tplname){
        self::$tpls[] = trim($tplname);
    }

    /**
     * Display the template
     *
     * @param string
     * @return mixed
     */
    public static function display(){
        extract(self::$data);
        if(is_array(self::$tpls) && count(self::$tpls)){
            foreach(self::$tpls as $tpl){
                include self::getTpl($tpl);
            }
        }
    }

    /**
     * Get the filename of the speical view
     *
     * @param string
     * @return string
     */
    public static function getTpl($tplname){
        $file = self::$tplRoot.$tplname.'.tpl.php';
        p($file);
        if(is_file($file)){
            return $file;
        }else{
            return false;
        }
    }

    /**
     * Debuge to dump the data in current template
     *
     * @param string
     * @return mixed
     */
    public static function dump(){
        var_dump(self::$data);
    }





    /**
     * 分页
     *
     * @param int    总记录数
     * @param int    当前页码
     * @param int    每页记录数
     * @param string 分页链接规则 {PAGE} 字符用于替换真正的页码值
     * @param int    页码集合，默认10页一组
     * @return string
     */
    public static function pages($total, $curr_page, $perpage = 20, $urlrule = '?page={PAGE}', $pagesets = 10) {

        function pageurl($urlrule, $page) {
            return str_replace('{PAGE}', $page, $urlrule);
        }

        $multipage = '';
        $page = $offset = $pages = 0;
        if($total > $perpage) {
            $page = $pagesets + 1;
            $offset = ceil($pagesets / 2 - 1);
            $pages  = ceil($total / $perpage);

            $from = $curr_page - $offset;
            $to   = $curr_page + $offset;
            $more = 0;
            if($page >= $pages) {
                $from = 2;
                $to   = $pages - 1;
            } else {
                if($from <= 1) {
                    $to   = $page - 1;
                    $from = 2;
                }elseif($to >= $pages) {
                    $from = $pages - ( $page - 2 );
                    $to   = $pages - 1;
                }
                $more = 1;
            }
            $multipage .= '<a class="a1">'.$total.'条</a>';
            if($curr_page > 0) {
                $multipage .= ' <a href="'.pageurl($urlrule, $curr_page-1).'" class="a1">上一页</a>';
                if($curr_page == 1) {
                    $multipage .= ' <span>1</span>';
                }elseif($curr_page > $pagesets / 2 + 1 && $more) {
                    $multipage .= ' <a href="'.pageurl($urlrule, 1).'">1</a> ... ';
                }else{
                    $multipage .= ' <a href="'.pageurl($urlrule, 1).'">1</a>';
                }
            }
            for($i = $from; $i <= $to; $i++) {
                if($i != $curr_page) {
                    $multipage .= ' <a href="'.pageurl($urlrule, $i).'">'.$i.'</a>';
                } else {
                    $multipage .= ' <span>'.$i.'</span>';
                }
            }
            if($curr_page < $pages) {
                if($curr_page < $pages - 5 && $more) {
                    $multipage .= ' ... <a href="'.pageurl($urlrule, $pages).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page + 1).'" class="a1">下一页</a>';
                } else {
                    $multipage .= ' <a href="'.pageurl($urlrule, $pages).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page + 1).'" class="a1">下一页</a>';
                }
            } elseif($curr_page == $pages) {
                $multipage .= ' <span>'.$pages.'</span> <a href="'.pageurl($urlrule, $curr_page).'" class="a1">下一页</a>';
            } else {
                $multipage .= ' <a href="'.pageurl($urlrule, $pages).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page + 1).'" class="a1">下一页</a>';
            }
        }
        return $multipage;
    }

}
?>